import Vue from 'vue'
import MPDataManager from "@/public/MPDataManager.js";

// 按钮失焦指令
Vue.directive('blur', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding, vnode) {
		el.onmouseup = (event) => {
			let target = event.target;
			if (target.nodeName == 'I' || target.nodeName == 'SPAN') {
				target = event.target.parentNode;
			}
			target.blur();
		}
	},
});


//判断是否有某个权限
const AuthorityUtil = {}
AuthorityUtil.checkAuthority = function(code) {
	// return true
	if (code == null || code == "") {
		return true;
	}
	let authoritys = MPDataManager.getData("SYS_authority")
	for (let key in authoritys) {
		let a = authoritys[key];
		if (a.module_code == code || a.function_code == code) {
			return true;
		}
	}
	return false;
};


// 权限指令 如果不如何权限 这个组件会被移除
Vue.directive('authority', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding, vnode) {
		let value = binding.value
		MPDataManager.addListener(el, "SYS_authority", function() {
			console.log("SYS_authority")
			console.log(value)
			if (!AuthorityUtil.checkAuthority(value)) {
				this.parentNode.removeChild(this);
			}
			MPDataManager.removeMyListener(this)
		})
	},
});

// 报送、维护的公司菜单列表里详情页的按钮显隐
Vue.directive('btn', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding) {
		const route = binding.arg || {
			query: {}
		}
		const text = route.query.text; //0,1,2
		const scope = binding.value || {
			row: {}
		}
		const status = scope.row.infsubAuditStatusCode; //01待提交,02待审核,05审核通过,06审核不通过,07退回
		const type = el.innerText; //'查看','新增','删除','修改','排序','提交'
		// console.log(type)
		let bool = true;
		// console.log(status)
		if (text == '0') {
			if (['查看'].includes(type)) bool = false; //fasle为显示按钮
		} else if (text == '1') {
			if (['查看', '修改'].includes(type)) bool = false;
		} else if (text == '2') {
			if (['新增', '删除', '修改', '排序'].includes(type)) bool = false;
			if ((status == '01'||status == '05'||status == '06'||status == '07') && type == '提交') bool = false;
		}
		if (status&&(status == '02' || bool)) {
			// el.parentNode.removeChild(el)
			el.style.display='none';
		}else{
			el.style.display='inline-block';
		}
		if(type=='新增'&&text=='0'){
			el.parentNode?.removeChild(el)
		}
	},
	componentUpdated(el, binding) {
		const route = binding.arg || {
			query: {}
		}
		const text = route.query.text; //0,1,2
		const scope = binding.value || {
			row: {}
		}
		const status = scope.row.infsubAuditStatusCode; //01,02
		const type = el.innerText; //'查看','新增','删除','修改','排序','提交'
		let bool = true;
		// console.log(status)
		if (text == '0') {
			if (['查看'].includes(type)) bool = false; //fasle为显示按钮
		} else if (text == '1') {
			if (['查看', '修改'].includes(type)) bool = false;
		} else if (text == '2') {
			if (['新增', '删除', '修改', '排序'].includes(type)) bool = false;
			if ((status == '01'||status == '05'||status == '06'||status == '07') && type == '提交') bool = false;
		}
		if (status&&(status == '02'|| bool)) {
			// el.parentNode.removeChild(el);
			el.style.display='none';
		}else{
			el.style.display='inline-block';
		}
		if(type=='新增'&&text=='0'){
			el.parentNode?.removeChild(el)
		}
	},
	
});

//审核修改标红
Vue.directive('red', {
	inserted(el, binding, vnode) {//mounted触发
		const [key,data]=binding.value;
		if(data.histList){
			let list=data.histList;
			if(list.length>0){
				list.map(it=>{
					if(it.chgColName==key){
						el.classList.add('red')
					}
				})
			}
		}
	},
	componentUpdated(el, binding, vnode) {//发生改变时触发
		const [key,data]=binding.value;
		if(data.histList){
			let list=data.histList;
			if(list.length>0){
				list.map(it=>{
					if(it.chgColName==key){
						el.classList.add('red')
					}
				})
			}
		}
	},
});

//拷贝一个json对象 （没有进行深度拷贝）
Vue.prototype.$CopyObj = function(obj) {
	if (obj == null) {
		return null;
	}
	let copy = {}
	for (let key in obj) {
		copy[key] = obj[key]
	}
	return copy;
}

Vue.prototype.$FormCheck = function(...formnams) {
	if (!formnams || formnams.length == 0) {
		formnams = ["form"]
	}
	//给个默认值 就是默认名字是form省事
	let yanzhengjieguo = true;
	let promises = [];
	formnams.forEach((formname) => {
		let form = this.$refs[formname]
		if (form == null) {
			return false;
		}
		promises.push(form.validate())
	})

	return Promise.all(promises).then((values) => {
		return true
	}).catch(() => {
		return false
	});
	return yanzhengjieguo;
}

//拷贝一个json对象 （没有进行深度拷贝）
Vue.prototype.$CopyObjByKey = function(obja, objcopy) {
	if (obja == null || objcopy == null) {
		return;
	}
	let copy = {}
	for (let key in obja) {
		if (objcopy[key] != undefined) {
			obja[key] = objcopy[key]
		}
	}
	return copy;
}



//对一个数字进行位数保留 如果余数是0 就显示为整数
Vue.prototype.FixedNum = function(num, weishu) {
	if (num == null || num == "") {
		return "";
	}
	num = Number(num)
	//
	if (weishu === undefined || weishu === null) {
		//默认2位小数
		weishu = 2
	}
	//10 的 小数的位数 次平方 Math.pow(x,y) x是底数，y是幂数
	let weishunum = Math.pow(10, weishu)
	//先乘weishunum后取整，把小数位的数字去除 Math.floor(x)向下取整
	num = Math.floor(num * weishunum)
	if (num % weishunum == 0) {
		return Math.floor(num / weishunum)
	} else {
		return num / weishunum;
	}
}

//水印
Vue.directive('watermark', {
	// 当自己被添加到父级节点的时候触发
	inserted(el, binding, vnode) {
		let text = binding.value.text;
    let font = binding.value.font || "16px Microsoft JhengHei";
    let textColor = binding.value.textColor || "rgba(215, 215, 215, 0.2)";
    let width = binding.value.width || 400;
    let height = binding.value.height || 200;
    let textRotate = binding.value.textRotate||-20;
    
    function addWaterMarker(parentNode){
        var can = document.createElement('canvas');
        parentNode.appendChild(can);
        can.width = width;
        can.height = height;
        can.style.display = 'none';
        var cans = can.getContext('2d');
        cans.rotate(textRotate * Math.PI / 180);
        cans.font = font;
        cans.fillStyle = textColor ;
        cans.textAlign = 'left';
        cans.textBaseline = 'Middle';
        cans.fillText(text, 0,  can.height);
        parentNode.style.backgroundImage = "url(" + can.toDataURL("image/png") + ")";
    }
    addWaterMarker(el)
	},
});